<?php
#=========================================================================================
#	Filename: getStatsClass.php
#	Note	: stats Class
#	Version : 1.0
#	Author  : Peter
#	Update  : 2010-04-23
#======================================================================================== 
class getStatsClass{
	//instance var
	private static $instance = NULL;
	
	//construct function
	private function __construct(){
	}
	
	//clone function 
	private function __clone(){
	}
	
	public static function getInstance(){
		if(self::$instance == NULL)
			self::$instance = new getStatsClass();
		return self::$instance;
	}
	
	/*
	 * Return All stats
	*/
	public function getStats(){
		//$stat_array = statClass::$state;
        $stat_array = statClass::getAllCountries();
		return array_keys($stat_array);
	}
	
	/*
	 * Return stats Section
	 */
	public function getStatsSection(){
		//$stat_array = statClass::$state;
        $stat_array = statClass::getAllCountries();
		$return_array = array();
		foreach($stat_array as $kk => $vv){
			foreach($vv  as $sectionKey => $sectionValue)
				$return_array[] = ucwords(strtolower($sectionKey));
		}
		
		return $return_array;
	}
	
	/*
	 * Return Country Name
	 * 
	 */
	public function getCountryName(){
		//$stat_array = statClass::$state;
        $stat_array = statClass::getAllCountries();
		$return_array = array();
		foreach($stat_array as $kk => $vv){
			foreach($vv  as $sectionKey => $sectionValue)
				$return_array[] = $sectionValue;
		}

        return $return_array;
	}

    public function getStatsByKey($stat,$section=null,$flag=null) {
        $result = array();
        //$stat_array = statClass::$state;
        $flag = (empty($flag)) ? true : false;
        $stat_array = statClass::getAllCountries($flag);
        if (empty($section)) {
            if (!isset($stat_array[$stat])) return $result;
            if (empty($flag)) {
                return $stat_array[$stat];
            }
            elseif($flag) {
                $data = $stat_array[$stat];
                if (!isset($result[$stat])) $result[$stat] = array();
                foreach($data as $section=>$section_val) {
                    foreach($section_val as $code=>$country) {
                        if ($country[1]==true) continue;
                        $result[$stat][$code] = $country[0];
                    }
                }
            }
            return $result;
        }
        else {
            if (!isset($stat_array[$stat]) || !isset($stat_array[$stat][$section]) ) return $result;
            return $stat_array[$stat][$section];
        }

        return $result;
    }

    public function getAllCountryByStat($stat,$flag=true) {
        $result = array();
        $tmp_result = array();
        //$stat_array = statClass::$state;
        $stat_array = statClass::getAllCountries($flag);

        foreach($stat_array[$stat] as $key=>$value) {
            foreach($value as $k2=>$v2) {
                if ($flag && (bool)$v2[1]==true) {
                    continue;
                }
                $result[$stat][$k2] = $v2;
            }
        }
        return $result;        
    }

	public function getRegion($region){
		//$stat_array = statClass::$state;
        $stat_array = statClass::getAllCountries();
		return isset($stat_array[$region]) ? ($stat_array[$region]) : array();
	}

	public function getSubByRegion($region){
		//$stat_array = statClass::$state;
        $stat_array = statClass::getAllCountries();
		return isset($stat_array[$region]) ? array_keys($stat_array[$region]) : array();
	}

	public function getCountryByRegionAndSub($region, $sub,$flag=true){
		//$stat_array = statClass::$state;
        $stat_array = statClass::getAllCountries($flag);
        if ($flag) {
            $result = array();
            foreach($stat_array as $region1=>$region_val) {
                if (!isset($result[$region1])) $result[$region1] = array();
                foreach($region_val as $sub1=>$sub_val) {
                    if (!isset($result[$region1][$sub1])) $result[$region1][$sub1] = array();
                    foreach($sub_val as $code1=>$code_val) {
                        if ((bool)$code_val[1]!=true) {
                            if (!isset($result[$region1][$sub1][$code1])) $result[$region1][$sub1][$code1] = null;
                            $name = $code_val[0];
                            $result[$region1][$sub1][$code1] = $name;
                        }
                    }
                }
            }
            return isset($result[$region][$sub]) ? $result[$region][$sub] : array();
        }
        else {
		    return isset($stat_array[$region][$sub]) ? $stat_array[$region][$sub] : array();
        }
	}

	public function getStatsFromXML(){
        /**
		$xmlPath = 'inc/countryCode.xml';
		$regions = parseCountryCode::parseXml($xmlPath);
        */
        $regions = statClass::getAllCountries();
		return array_keys($regions);
	}

    static public function getRegions($flag) {
        return statClass::getAllCountries($flag);
    }


}
?>
